package com.mygdx.game.Sprite;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.*;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.utils.Array;
import com.mygdx.game.Scenes.ActiveCharacter;
import com.mygdx.game.Scenes.Hud;
import com.mygdx.game.Screens.PlayScreen;

public class Mario extends Sprite {
    public World world;
    public Body b2Body;
    public ActiveCharacter activeCharacter;
    public static float statetime = 0;
    TextureRegion[] walkFrame;
    TextureRegion[] walkFrame2;
    TextureRegion currentFrame;
    TextureRegion currentFrame2;
    Animation aniRight;
    Animation aniRight2;
    public static float width;
    public static float heigth;

    private int Radius;
    private int x;
    private int y;
    private TextureRegion marioStand;
    private Animation marioRun;
    private Texture texture;

    public Mario(World world,int Radius,int x,int y){

        this.Radius=Radius;
        this.x=x;
        this.y=y;
//        texture =new Texture("Character/Protagonist (1).png");
//        marioStand = new TextureRegion(texture);

        this.world = world;
        defineMario();


        setBounds(0,0,30,30);
//        setRegion(marioStand);


    }
    public void update(float dt){

        setPosition(b2Body.getPosition().x-getWidth()/2 ,b2Body.getPosition().y-getHeight()/2);

        Texture[] temp = new Texture[4];
        walkFrame = new TextureRegion[4];
        for (int i = 0; i < 4; i++) {
            temp[i] = new Texture(Gdx.files.internal("Character/Protagonist" + " (" + (4 - i) + ").png"));
            width = temp[i].getWidth();
            heigth = temp[i].getHeight();
            walkFrame[i] = new TextureRegion(temp[i], 0, 0, 100, 100);
        }
        Texture[] temp2 = new Texture[4];
        walkFrame2 = new TextureRegion[4];
        for (int i = 0; i < 4; i++) {
            temp2[i] = new Texture(Gdx.files.internal("Character/Protagonist" + " (" + (4 - i) + ").png"));
            width = temp2[i].getWidth();
            heigth = temp2[i].getHeight();
            walkFrame2[i] = new TextureRegion(temp[i], 0, 0, 100, 100);
            //反方向
            walkFrame2[i].flip(true,false);
        }
        aniRight = new Animation(0.2f, walkFrame);
        aniRight2 = new Animation(0.2f, walkFrame2);

        statetime += Gdx.graphics.getDeltaTime();
        currentFrame = (TextureRegion) aniRight.getKeyFrame(statetime,true);
        currentFrame2 = (TextureRegion) aniRight2.getKeyFrame(statetime, true);
        setPosition(b2Body.getPosition().x - getWidth() / 2, b2Body.getPosition().y - getHeight() / 2);
        setRegion(currentFrame);
        if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
           setRegion(currentFrame2);
       }
       else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
           setRegion(currentFrame);
       }
       else if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
            setRegion(currentFrame2);
        }
       else if(Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
            setRegion(currentFrame);
        }
       else {
            setRegion(currentFrame);
        }

    }
    public void handleInput(float dt) {
        if (Gdx.input.isKeyPressed(Input.Keys.UP) && this.b2Body.getLinearVelocity().y<=100f) {
            this.b2Body.applyLinearImpulse(new Vector2(0, 20f), this.b2Body.getWorldCenter(), true);
        } else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT) && this.b2Body.getLinearVelocity().x<=100f) {
            this.b2Body.applyLinearImpulse(new Vector2(20f, 0), this.b2Body.getWorldCenter(), true);
        } else if (Gdx.input.isKeyPressed(Input.Keys.DOWN) && this.b2Body.getLinearVelocity().y>=-100f) {
            this.b2Body.applyLinearImpulse(new Vector2(0, -20f), this.b2Body.getWorldCenter(), true);
        } else if (Gdx.input.isKeyPressed(Input.Keys.LEFT) && this.b2Body.getLinearVelocity().x>=-100f) {
            this.b2Body.applyLinearImpulse(new Vector2(-20f, 0), this.b2Body.getWorldCenter(), true);
        } else {

            //mario.b2Body.setUserData(hud.activeCharacter);

        }
    }
    public void defineMario(){
        BodyDef bdef = new BodyDef();
        bdef.position.set(x,y);
        bdef.type = BodyDef.BodyType.DynamicBody;
        b2Body = world.createBody(bdef);
        FixtureDef fdef = new FixtureDef();
        CircleShape shape = new CircleShape();
        shape.setRadius(Radius);
        fdef.shape = shape;
        b2Body.createFixture(fdef);

    }

}
